package com.hyk.controller;

import com.hyk.exception.GlobalException;
import com.hyk.ann.ResponseResult;
import com.hyk.dto.ApiResult;
import com.hyk.entity.request.User;
import com.hyk.enums.ApiEnum;
import com.hyk.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Map;


@RestController
@Api(tags = "login和hello接口")
@ResponseResult
public class HelloController {

    static Map<String,String> map = new HashMap<>();

    static {
        map.put("zhangsan","123456");
        map.put("list","abc");
    }

    @ApiOperation("hello接口")
    @RequestMapping("/hello")
    public User print() throws FileNotFoundException {

        User user = new User();
        user.setUsername("zhangsan");
        user.setPassword("123456");
        return user;

    }

    @ApiOperation("hello1接口")
    @RequestMapping("/hello1")
    public ApiResult print1(){

        //int i = 1/0;

        //throw new ApiEnum(FAIL_NAME_OR_PASSWORD_ERROR);

        return R.ok("hello world");
    }

//    @RequestMapping("/hello2")
//    @ApiOperation("hello2接口")
//    public ApiResult print2(){
//
//        int[] a = new int[2];
//        a[2] = 1;
//
//        return N.no("hello ShenZhen");
//    }

    @RequestMapping(value = "/login",method = RequestMethod.POST)
    @ApiOperation(value = "登录接口")
    public ApiResult login(@RequestBody User user){
        String mapPassword = map.get(user.getUsername());

        if (mapPassword == null || "".equals(mapPassword)){
            throw new GlobalException(ApiEnum.FAIL_NAME_OR_PASSWORD_ERROR);
        }

        if (!mapPassword.equals(user.getPassword())){
            throw new GlobalException(ApiEnum.FAIL_NAME_OR_PASSWORD_ERROR);
        }
        return R.ok();
    }

}
